package com.martin.dms.model.hib;

import java.util.HashSet;
import java.util.Set;

import com.martin.dms.model.Client;
import com.martin.dms.model.Purchase;

/**
 * Hibernate client implementation.
 * 
 * @author mtsvetkov
 */
public class ClientHib implements Client {

    private Long id;

    private String firstName;
    private String lastName;
    private String address;
    private String email;
    private String phoneNumber;
    private String username;
    private String password;

    private Set<Purchase> purchases = new HashSet<Purchase>();

    /**
     * Default constructor.
     */
    public ClientHib() {
    }

    /**
     * {@inheritDoc}
     */
    public Long getId() {
        return id;
    }

    /**
     * {@inheritDoc}
     */
    public String getAddress() {
        return address;
    }

    /**
     * Setter for the client's address.
     * 
     * @param address - the client's address.
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * {@inheritDoc}
     */
    public String getEmail() {
        return email;
    }

    /**
     * Setter for the client's e-mail.
     * 
     * @param address - the client's e-mail.
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * {@inheritDoc}
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * Setter for the client's first name.
     * 
     * @param address - the client's first name.
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    /**
     * {@inheritDoc}
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * Setter for the client's last name.
     * 
     * @param address - the client's last name.
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    /**
     * {@inheritDoc}
     */
    public String getPhoneNumber() {
        return phoneNumber;
    }

    /**
     * Setter for the client's phone number.
     * 
     * @param address - the client's phone number.
     */
    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    /**
     * {@inheritDoc}
     */
    public String getUsername() {
        return username;
    }

    /**
     * Setter for the client's username.
     * 
     * @param address - the client's username.
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * {@inheritDoc}
     */
    public String getPassword() {
        return password;
    }

    /**
     * Setter for the client's password.
     * 
     * @param address - the client's password.
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * {@inheritDoc}
     */
    public Set<Purchase> getPurchases() {
        return purchases;
    }

    /**
     * Setter for the history of all client's purchases.
     * 
     * @param address - the client's purchases.
     */
    public void setPurchases(Set<Purchase> purchases) {
        this.purchases = purchases;
    }

    /**
     * Adds a new purchase to the client.
     * 
     * @param purchase to add.
     */
    public void addPurchase(PurchaseHib purchase) {
        purchases.add(purchase);
        purchase.setClient(this);
    }

    /**
     * {@inheritDoc}
     */
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof ClientHib)) {
            return false;
        }
        final ClientHib client = (ClientHib) obj;
        if (client.getId() != null && this.getId() != null) {
            return client.getId().equals(this.getId());
        }
        return false;
    }

    /**
     * {@inheritDoc}
     */
    public int hashCode() {
        int result = 17;
        if (getId() != null) {
            result += 37 * result + getId().hashCode();
        }
        return result;
    }
}
